package com.base.cn.platform.os.controller.upload;

import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.upload.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件上传接口
 *
 * @author s.li
 * @create 2018-10-01-11:51
 */
@RestController
@RequestMapping("/api/upload")
public class ApiFileUploadController extends ApiBaseController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * APP上传图片
     * @param multipartFile 文件对象
     * @return Map<String,Object>
     * @throws Exception
     */
    @PostMapping(value = "/uploadImgFile")
    public Map<String,Object> appUploadImg(@RequestParam("fileupload") MultipartFile[] multipartFile,
                                           @RequestParam("params") String params) throws Exception{
        List<Map<String,byte[]>> imageDataList = new ArrayList<>();
        for (MultipartFile file : multipartFile){
            byte[] byArr = file.getBytes();
            String fileName = file.getOriginalFilename();
            String key =fileName.substring(fileName.lastIndexOf("."));
            Map<String,byte[]> fileMap = new HashMap<>();
            fileMap.put(key,byArr);
            imageDataList.add(fileMap);
        }
        if(ObjectUtils.isNotEmpty(imageDataList)){
            return fileUploadService.updateByteFileData(imageDataList,params);
        }
        return ResultUtil.ERROR("文件上传失败");
    }
}
